package com.ofa.action;

import java.io.Serializable;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.ofa.base.BaseAction;
import com.ofa.base.Pagination;
import com.ofa.entity.Module;
import com.ofa.entity.Person;
import com.ofa.entity.Role;
import com.ofa.entity.User;
import com.ofa.entity.UserRole;
import com.ofa.service.ModuleService;
import com.ofa.service.PersonService;
import com.ofa.service.RoleService;
import com.ofa.service.UserService;
import com.ofa.util.CommonUtils;

public class UserAction extends BaseAction {
	
	private int maxResults = 10;
	private int firstResult = 0;
	
	private Integer id;
	private List<Serializable> ids;
	private Integer personId;
	private Integer roleId;

	private User user;
	private UserService userService;
	private List<User> userList;
	
	private Person person;
	private PersonService personService;
	private List<Person> personList;
	
	private Role role;
	private RoleService roleService;
	private List<Role> roleList;
	private UserRole userRole;
	private List<UserRole> userRoleList;
	
	private Module module;
	private ModuleService moduleService;
	private List<Module> moduleList;
	
	private Pagination<User> pagination;
	private Pagination<Person> personPagination;

	
	
	@Override
	public String execute(){
		String offset = ServletActionContext.getRequest().getParameter("pager.offset");
		if(CommonUtils.isNotEmpty(offset))
			firstResult = Integer.valueOf(offset);
		
		pagination = userService.queryUsers(user, firstResult, maxResults);
		return SUCCESS;
	}
	
	public String selectPersonList(){
		String offset = ServletActionContext.getRequest().getParameter("pager.offset");
		if(CommonUtils.isNotEmpty(offset))
			firstResult = Integer.valueOf(offset);
		
		personPagination = personService.queryPersons(person, firstResult, maxResults);
		return "selectPersonList";
	}

	public String preSave(){
		personPagination = personService.queryPersons(person, firstResult, Integer.MAX_VALUE);
		return "preSave";
	}
	
	public String save(){
		userService.save(user, personId);
		return "save";
	}

	public String preUpdate(){
		user = userService.get(id);
		personPagination = personService.queryPersons(person, firstResult, Integer.MAX_VALUE);
		return "preUpdate";
	}
	
	public String update(){
		userService.update(user);
		return "update";
	}
	
	public String delete(){		
		userService.delete(id);
		return "delete";
	}
	
	public String get(){
		user = userService.get(id);
		return "get";
	}
	
	public String preDistribute(){
		userRoleList = roleService.queryRolesOfUser(id);
		return "preDistribute";
	}
	
	public String openDistribute(){
		roleList = roleService.queryRoles(role, firstResult, Integer.MAX_VALUE).getData();
		return "openDistribute";
	}
	
	public String distribute(){
		user = userService.get(id);
		role = roleService.get(roleId);

		userRole.setUser(user);
		userRole.setRole(role);
		roleService.save(userRole);
		return "distribute";
	}
	
	public String openAuthorization(){
		moduleList = moduleService.queryModules(module, 0);//queryRoles(role, firstResult, Integer.MAX_VALUE).getData();
		return "openAuthorization";
	}

	
	

	
	
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}


	public List<Person> getPersonList() {
		return personList;
	}
	public void setPersonList(List<Person> personList) {
		this.personList = personList;
	}

	public Pagination<User> getPagination() {
		return pagination;
	}
	public void setPagination(Pagination<User> pagination) {
		this.pagination = pagination;
	}

	public int getMaxResults() {
		return maxResults;
	}
	public void setMaxResults(int maxResults) {
		this.maxResults = maxResults;
	}

	public int getFirstResult() {
		return firstResult;
	}
	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}

	public List<Serializable> getIds() {
		return ids;
	}
	public void setIds(List<Serializable> ids) {
		this.ids = ids;
	}

	public Integer getPersonId() {
		return personId;
	}
	public void setOrgId(Integer personId) {
		this.personId = personId;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public List<User> getUserList() {
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	public Pagination<Person> getPersonPagination() {
		return personPagination;
	}

	public void setPersonPagination(Pagination<Person> personPagination) {
		this.personPagination = personPagination;
	}

	public void setPersonId(Integer personId) {
		this.personId = personId;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public UserRole getUserRole() {
		return userRole;
	}

	public void setUserRole(UserRole userRole) {
		this.userRole = userRole;
	}

	public List<UserRole> getUserRoleList() {
		return userRoleList;
	}

	public void setUserRoleList(List<UserRole> userRoleList) {
		this.userRoleList = userRoleList;
	}

	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public ModuleService getModuleService() {
		return moduleService;
	}

	public void setModuleService(ModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public List<Module> getModuleList() {
		return moduleList;
	}

	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}
	
	
	
}
